var mongodb = require('mongodb');
var BSON = require('mongodb').pure().BSON;
var ObjectID = require('mongodb').ObjectID;
/*
if(process.env.VCAP_SERVICES){
    var env = JSON.parse(process.env.VCAP_SERVICES);
    var mongo = env['mongodb-1.8'][0].credentials;
}
else{
    var mongo = {
            "hostname":"ds039078.mongolab.com",
            "port":39078,
            "username":"admin",
            "password":"mta2013",
            "db":"activiteam"
    };
}

var generate_mongo_url = function(obj){
    obj.hostname = (obj.hostname || 'localhost');
    obj.port = (obj.port || 27017);
    obj.db = (obj.db || 'test');

    if(obj.username && obj.password){
        return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
    }
    else{
        return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
    }
};
*/
var mongourl = process.env.MONGOLAB_DB ? process.env.MONGOLAB_DB : 'mongodb://admin:mta2013@ds039078.mongolab.com:39078/activiteam';//generate_mongo_url(mongo);


/// creating a new message
/// INPUT: the request must contain {to,toEmail,from,fromEmail,date,text
/// OUTPUT: {created: ok/error}
exports.createMessage = function(req, res) {
    mongodb.connect(mongourl, function(err, db) {
        if (err) {
            console.log(err);
            res.send({"created" : "error"});
            return;
        } else {
            db.collection('messages', function(err, collection) {
                if (err) {
                    res.send({"created" : "error"});
                    db.close();
                    return;
                } else {
                    collection.insert({
                        "to":req.body.to,
                        "toEmail":req.body.toEmail,
                        "from":req.body.from,
                        "fromEmail":req.body.fromEmail,
                        "date":new Date(req.body.date),
                        "text":req.body.text
                    }
                    , function(err, result) {
                        if (err) {
                            res.send({"created" : "error"});
                            db.close();
                            return;
                        } else {
                            res.send({"created" : "ok"});
                            db.close();
                            return;
                        }
                    });
                }
            });
        }
    });
};


//delete a message
//INPUT: request should contain the id of the requested message. session must have the email of reciever.
//OUTPUT: {delete: success/error/not logged}
//Note: can delete only the messages to the user in session
exports.deleteMessage = function(req, res) {
    //check first that the user is in session:
    if (!req.session.myEmail){
        res.send({"delete":"not logged"});
        return;
    }
    else{
        //connect to the db:
        mongodb.connect(mongourl,function(err,db){
            if (err){
                res.send({"delete":"error"});
                db.close();
                return;
            }
            //getting the messages collection:
            db.collection('messages',function(err,collection){
                if (err)
                {
                    res.send({"delete":"error"});
                    db.close();
                    return;
                }
                //delete the message
                collection.remove({"toEmail":req.session.myEmail,"_id":ObjectID(req.body.id)},1 
                        , function(err,user){
                    db.close();
                    if(err){
                        res.send({"delete":"error"});
                        return;
                    } else {
                        res.send({"delete":"success"});
                        return;
                    }
                });
            });
        });
    } 
};
